package com.mayuran19.portal.accounting.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mayuran19.portal.accounting.dao.ExpenseDetailDAO;
import com.mayuran19.portal.accounting.dao.ExpenseHeaderDAO;
import com.mayuran19.portal.accounting.database.domain.ExpenseDetail;
import com.mayuran19.portal.accounting.database.domain.ExpenseHeader;
import com.mayuran19.portal.accounting.service.ExpenseDetailService;
import com.mayuran19.portal.accounting.service.ExpenseHeaderService;
import com.mayuran19.portal.common.database.domain.Product;
import com.mayuran19.portal.common.ui.pagination.PagedResultSet;
import com.mayuran19.portal.common.ui.pagination.Pager;

@Service("expenseHeaderService")
public class ExpenseHeaderServiceImpl implements ExpenseHeaderService {
	private ExpenseHeaderDAO expenseHeaderDAO;
	private ExpenseDetailService expenseDetailService;
	private ExpenseDetailDAO expenseDetailDAO;

	@Autowired
	public void setExpenseDetailDAO(ExpenseDetailDAO expenseDetailDAO) {
		this.expenseDetailDAO = expenseDetailDAO;
	}

	@Autowired
	public void setExpenseHeaderDAO(ExpenseHeaderDAO expenseHeaderDAO) {
		this.expenseHeaderDAO = expenseHeaderDAO;
	}

	@Autowired
	public void setExpenseDetailService(
			ExpenseDetailService expenseDetailService) {
		this.expenseDetailService = expenseDetailService;
	}

	public ExpenseHeader save(ExpenseHeader t) {
		return this.expenseHeaderDAO.save(t);
	}

	public ExpenseHeader update(ExpenseHeader t) {
		return this.expenseHeaderDAO.update(t);
	}

	public ExpenseHeader findById(Long id) {
		return this.expenseHeaderDAO.findById(id);
	}

	public ExpenseHeader findById(Long id, String... include) {
		return this.expenseHeaderDAO.findById(id, include);
	}

	public ExpenseHeader delete(ExpenseHeader t) {
		return this.expenseHeaderDAO.delete(t);
	}

	public List<ExpenseHeader> findAll() {
		return this.expenseHeaderDAO.findAll();
	}

	public List<ExpenseHeader> findAll(Pager pager) {
		return this.expenseHeaderDAO.findAll(pager);
	}

	public PagedResultSet paginate(Pager pager) {
		return this.expenseHeaderDAO.paginate(pager);
	}

	public Integer getAllCount() {
		return this.expenseHeaderDAO.getAllCount();
	}

	public Boolean isUnique(Long id, String fieldName, String fieldValue) {
		return this.expenseHeaderDAO.isUnique(id, fieldName, fieldValue);
	}

	@Transactional
	public ExpenseHeader addProductToExpense(Product product,
			ExpenseHeader expenseHeader, ExpenseDetail expenseDetail) {
		// first add the expenseHeader
		if (expenseHeader.getId() == null
				|| expenseHeader.getId().longValue() == 0l) {
			expenseHeader.setTotal(expenseDetail.getUnitPrice().multiply(
					expenseDetail.getQuantity()));
			expenseHeader.setTax(expenseDetail.getTaxTotal());
			expenseHeader.setNetTotal(expenseDetail.getNetTotal());
			this.save(expenseHeader);
		} else {
			expenseHeader.setTotal(expenseHeader.getTotal().add(
					expenseDetail.getTotal()));
			expenseHeader.setTax(expenseHeader.getTax().add(
					expenseDetail.getTaxTotal()));
			expenseHeader.setNetTotal(expenseHeader.getNetTotal().add(
					expenseDetail.getNetTotal()));
			this.update(expenseHeader);
		}
		expenseDetail.setExpenseHeader(expenseHeader);
		expenseDetail.setProduct(product);
		expenseDetail.setTotal(expenseDetail.getUnitPrice().multiply(
				expenseDetail.getQuantity()));
		this.expenseDetailService.save(expenseDetail);
		expenseHeader.setExpenseDetails(this.expenseDetailDAO.findByProperty(
				"expenseHeader.id", expenseHeader.getId()));
		return expenseHeader;
	}

	public ExpenseHeader refresh(ExpenseHeader t) {
		return this.expenseHeaderDAO.refresh(t);
	}

}
